﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class LPU : IEquatable<LPU>
    {
        public void ImportList(List<Classes.LPU> lpuList)
        {
            var mdb = new MedregDBDataContext();

            var sNom = new Classes.Nomenclature();
            var sLev = new Classes.Level();
            
            var nomen = new Nomenclature();
            nomen.ImportList(sNom.getNomeclatureList(lpuList));
            var level = new Level();
            level.ImportList(sLev.getLevelList(lpuList));


            var ListLPU = new List<LPU>();

            foreach (var lpu in lpuList)
            {
                LPU lpu1 = new LPU 
                { 
                    ID = lpu.ID, 
                    Name = lpu.Name, 
                    INN = lpu.INN, 
                    KPP = lpu.KPP, 
                    LPULevel = lpu.LPULevel.ID, 
                    Nomen = lpu.Nomen.ID,
                    //Employee = lpu.Employee.ID
                    RealID = lpu.RealID
                };
                
                LPU query;

                if (mdb.LPU.LongCount() == 0)
                {
                    mdb.LPU.InsertOnSubmit(lpu1);
                }
                else
                {
                    ListLPU.AddRange(mdb.LPU);
                    try
                    {
                        query = mdb.LPU.Single(l => l.ID == lpu1.ID & l.Name == lpu1.Name & l.INN == lpu1.INN);
                        query.KPP = lpu1.KPP;
                        query.LPULevel = lpu1.LPULevel;
                        query.Nomen = lpu1.Nomen;
                        query.RealID = lpu1.RealID;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        
                        if (!ListLPU.Contains(lpu1))
                        {
                            mdb.LPU.InsertOnSubmit(lpu1);
                            ListLPU.Add(lpu1);
                        }

                        
                    }
                }
            }
            mdb.SubmitChanges();
        }

        public bool Equals(LPU other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            var k = this.INN.Equals(other.INN);
            if (i & j & k)
            {
                return true;
            }
            return false;
        }
        public Guid GetRealID (Classes.LPU lpu)
        {
            var mdb = new MedregDBDataContext();

            LPU query;
            var result = new Guid();
            try
            {
                query = mdb.LPU.Single(l => l.ID == lpu.ID & l.Name == lpu.Name & l.INN == lpu.INN);
                result = query.RealID;
            }
            catch (ArgumentNullException e)
            {
            }
            catch (InvalidOperationException e)
            {
                result = Guid.Empty;
            }
            return result;
        }

        public List<LPU> GetLPUList()
        {
            return (new MedregDBDataContext()).LPU.ToList();
        }


    }
}
